/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfophone.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfophone.entity.CampusinfoPhone;
import com.lczyfz.campusinfo.modules.campusinfophone.service.CampusinfoPhoneService;

/**
 * 联系方式Controller
 * @author zms
 * @version 2018-05-14
 */
@Controller
@RequestMapping(value = "${adminPath}/campusinfophone/campusinfoPhone")
public class CampusinfoPhoneController extends BaseController {

	@Autowired
	private CampusinfoPhoneService campusinfoPhoneService;
	
	@ModelAttribute
	public CampusinfoPhone get(@RequestParam(required=false) String id) {
		CampusinfoPhone entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = campusinfoPhoneService.get(id);
		}
		if (entity == null){
			entity = new CampusinfoPhone();
		}
		return entity;
	}
	
	@RequiresPermissions("campusinfophone:campusinfoPhone:view")
	@RequestMapping(value = {"list", ""})
	public String list(CampusinfoPhone campusinfoPhone, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CampusinfoPhone> page = campusinfoPhoneService.findPage(new Page<CampusinfoPhone>(request, response), campusinfoPhone); 
		model.addAttribute("page", page);
		return "modules/campusinfophone/campusinfoPhoneList";
	}

	@RequiresPermissions("campusinfophone:campusinfoPhone:view")
	@RequestMapping(value = "form")
	public String form(CampusinfoPhone campusinfoPhone, Model model) {
		model.addAttribute("campusinfoPhone", campusinfoPhone);
		return "modules/campusinfophone/campusinfoPhoneForm";
	}

	@RequiresPermissions("campusinfophone:campusinfoPhone:edit")
	@RequestMapping(value = "save")
	public String save(CampusinfoPhone campusinfoPhone, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, campusinfoPhone)){
			return form(campusinfoPhone, model);
		}
		campusinfoPhoneService.save(campusinfoPhone);
		addMessage(redirectAttributes, "保存联系方式成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfophone/campusinfoPhone/?repage";
	}
	
	@RequiresPermissions("campusinfophone:campusinfoPhone:edit")
	@RequestMapping(value = "delete")
	public String delete(CampusinfoPhone campusinfoPhone, RedirectAttributes redirectAttributes) {
		campusinfoPhoneService.delete(campusinfoPhone);
		addMessage(redirectAttributes, "删除联系方式成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfophone/campusinfoPhone/?repage";
	}

}